package saucaca.fragment;

import lemon.kenhnhacvn.BaseEngine;
import lemon.kenhnhacvn.MainActivity;
import lemon.kenhnhacvn.R;

import org.json.JSONException;

import saucaca.JSON.JsonReader;
import saucaca.JSON.WServiceLib;
import saucaca.Object.HomeData;
import saucaca.Util.TagName;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.res.Resources;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;


public class AccountForgetpassFragment extends Fragment {
	String TAG = AccountRegisterFragment.class.getSimpleName();
	private MainActivity mActivity = null;
	LinearLayout btnFogetpass;
	EditText edtUser;
	Resources resources;
	String user, pass, repass;
	TextView txtConfirm;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	@SuppressLint("NewApi")
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//		getActivity().getActionBar().hide();
		resources = getResources();
		View view = inflater.inflate(R.layout.account_forgetpass, container, false);
		findViewById(view);
		mActivity.myActionBar.setMenu(R.string.forgetpass, false, false);
		return view;
	}
	public void findViewById(View view) {
		btnFogetpass = (LinearLayout) view.findViewById(R.id.forgetpass_getnewpass);
		edtUser = (EditText) view.findViewById(R.id.forgetpass_edtUser);
		btnFogetpass.setOnClickListener(clickListener);
		txtConfirm = (TextView) view.findViewById(R.id.forgetpass_textConfirm);
	}

	View.OnClickListener clickListener = new OnClickListener() {
		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.forgetpass_getnewpass:
				registerAccount();
				break;
			default:
				break;
			}
		}
	};

	private void registerAccount() {
		user = edtUser.getText().toString();
		if (user.equals("")) {
			TagName.showToastShort(mActivity, resources.getString(R.string.err_register_input_null));
		} else
			new GetFromServices().execute();

	}

	class GetFromServices extends AsyncTask<String, String, String> {
		private ProgressDialog progressDialog = new ProgressDialog(getActivity());

		@Override
		protected String doInBackground(String... param) {
			String str = WServiceLib.forgetpass(user);
			BaseEngine.showLogformLongString(str);
			// String request = BaseEngine.getRequest(getActivity(), str);
			return str;
		}

		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			// progressDialog.setTitle("");
			if (!progressDialog.isShowing())
				progressDialog.show();
		}

		@Override
		protected void onPostExecute(String result) {
			super.onPostExecute(result);
			if (progressDialog.isShowing())
				progressDialog.hide();
			if (result != null) {
				try {
					JsonReader.readHeader(result);
				} catch (JSONException e) {
					e.printStackTrace();
				}
			}
			if (HomeData.headerObject.getCODE().equals("0")) {
				edtUser.setText("");
				txtConfirm.setText(HomeData.headerObject.getMESSAGE());
				// mActivity.showprevousFragment();
			} else
				TagName.showToastShort(mActivity, HomeData.headerObject.getMESSAGE());
		}
	}

	private void switchFragment(String fragment) {
		if (getActivity() == null)
			return;
		else if (getActivity() instanceof MainActivity) {
			MainActivity a = (MainActivity) getActivity();
			a.switchContent(fragment);
		}
	}

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		if (activity instanceof MainActivity) {
			mActivity = (MainActivity) activity;
		}
	}
}